package com.itlong.cloud.controller.user;


import com.itlong.cloud.POJO.DTO.property.PropertyProjectGetPropertyInfoDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyProjectPropertyInfoVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.PropertyInfoTypeEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.project.property.IPropertyPropertyInfoServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * <desc>
 *    物业投诉、物业报修 管理控制器
 * </desc>
 *
 * @createDate 2017-10-16
 */
@RestController
@RequestMapping("user/propertyInfo")
public class PropertyRepairAndComplainController extends BaseController {

    @Autowired
    IPropertyPropertyInfoServiceClient iPropertyPropertyInfoServiceClient;



    /**
     * <desc>
     *      查询物业信息
     * <desc/>
     * @param  pageDTO 查询物业信息数据传输对象
     * @return 物业信息 返回对象
     * @author ZiXuan
     * @createDate 2017-10-16
     */
    @RequestMapping(path = "/getPage",method = RequestMethod.POST)
    @ResponseBody
    public Object getPage(PropertyProjectGetPropertyInfoDTO pageDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyProjectGetPropertyInfoDTO.class, request);
            if (null == pageDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }

            //项目编号
            if(StringUtils.isBlank(pageDTO.getProjectId())){
                return new MessageVO(PropertyErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }

            //物业信息 类型判断
            if((null != pageDTO.getPropertyType()) && (pageDTO.getPropertyType() != PropertyInfoTypeEnum.REPAIR_INFO.getType() &&
                    pageDTO.getPropertyType() != PropertyInfoTypeEnum.COMPLAIN_INFO.getType())){
                return new MessageVO(PropertyErrorCodeEnum.PROJECT_PROPERTY_INFO_TYPE_INVALID_ERR.getErrorCode());
            }

            Page<PropertyProjectPropertyInfoVO> page = new Page<>();
            List<Integer> propertyTypeList = new ArrayList<>();

            if (null == pageDTO.getPropertyType()){        //默认查询报修和投诉
                propertyTypeList.add(PropertyInfoTypeEnum.REPAIR_INFO.getType());
                propertyTypeList.add(PropertyInfoTypeEnum.COMPLAIN_INFO.getType());
            }
            else
            {
                propertyTypeList.add(pageDTO.getPropertyType());
            }

            pageDTO.setPropertyTypeList(propertyTypeList);
            page = iPropertyPropertyInfoServiceClient.getPage(pageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), page);

        } catch (Exception e) {
            throw new DataAccessException("【物业_住户管理_投诉反馈】分页查询失败", e);
        }
    }


    /**
     * <desc>
     *      修改物业信息
     * <desc/>
     * @param  propertyDTO 修改物业信息数据传输对象
     * @return
     * @author ZiXuan
     * @createDate 2017-10-26
     */
    @LoggerInfo(operateDesc = LogDescConstant.UPDATE_PROPERTYINFO_LOG_DESC)
    @RequestMapping(path = "/updatePropertyInfo",method = RequestMethod.POST)
    @ResponseBody
    public Object updatePropertyInfo(PropertyProjectGetPropertyInfoDTO propertyDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyProjectGetPropertyInfoDTO.class, request);
            if (null == propertyDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //物业信息编号
            if(StringUtils.isBlank(propertyDTO.getPropertyInfoId())){
                return new MessageVO(PropertyErrorCodeEnum.PROJECT_PROPERTY_INFO_ID_EMPTY_ERR.getErrorCode());
            }
            //回复内容
            if(StringUtils.isBlank(propertyDTO.getReplyContent())){
                return new MessageVO(PropertyErrorCodeEnum.PROJECT_PROPERTY_REPLY_INFO_INVALID_ERR.getErrorCode());
            }
            iPropertyPropertyInfoServiceClient.updatePropertyInfo(propertyDTO);
            return new MessageVO(propertyDTO.getPropertyInfoId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());

        } catch (Exception e) {
            throw new DataAccessException("【物业_住户管理_投诉反馈】修改物业信息失败", e);
        }
    }
}